package com.JZ.QM.controller;

import com.JZ.QM.common.entity.CombinationBaseInfoEntity;
import com.JZ.QM.common.utils.CommonResult;
import com.JZ.QM.dto.CombinationBaseInfoDto;
import com.JZ.QM.dto.CombinationProductDto;
import com.JZ.QM.service.CombinationBaseInfoService;
import com.alibaba.druid.sql.visitor.ExportParameterizedOutputVisitor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author lyp
 * @email lyp@gmail.com
 * @date 2020/6/16 14:37
 */


@RestController
@RefreshScope
@Slf4j
@RequestMapping(value="/combinationBaseInfo")
public class CombinationBaseInfoController {

    @Resource
    CombinationBaseInfoService combinationBaseInfoService;

    /**
     * 获取组合产品列表
     * @return
     */
    @RequestMapping("/allcombinationBaseInfo")
    public CommonResult allcombinationBaseInfo() throws ParseException {
        //获取组合基本信息列表
        List<CombinationBaseInfoEntity> combinationBaseInfoList = combinationBaseInfoService.list();
        //传输DTO的list
        List<CombinationBaseInfoDto> combinationBaseInfoDtoList = new ArrayList<>();

        //将Entity转换为DTO
        for (CombinationBaseInfoEntity comBaseEntity:combinationBaseInfoList){
            combinationBaseInfoDtoList.add(combinationBaseInfoService.transform(comBaseEntity));
        }
        if (!combinationBaseInfoDtoList.isEmpty()){
            return CommonResult.success().put("combinationBaseInfoDtoList",combinationBaseInfoDtoList);
        } else {
            return CommonResult.error(201,"组合产品列表为空");
        }
    }


    /**
     * 新增组合
     * @param combinationProduct
     */
    @PostMapping("/insertCombinationBaseInfo")
    public CommonResult insertCombinationProduct(@Valid @RequestBody CombinationProductDto combinationProduct){
        //保存组合产品
        boolean saveFlag = combinationBaseInfoService.saveCombinationProduct(combinationProduct);

        if (saveFlag) {
            return CommonResult.success("新建成功");
        } else {
            return CommonResult.error(201, "保存失败");
        }
    }

    /**
     * 组合上架
     * @param comBaseId 组合基本信息id
     * @return
     */
    @RequestMapping("/combinationOnShelf")
    public CommonResult combinationOnShelf(Long comBaseId) {
        //组合是否存在
        CombinationBaseInfoEntity combinationBaseInfoEntity;
        //组合是否上架
        Integer combinationOnShelfFlag;
        //是否修改组合
        Integer modifyCombationState;

        //获取组合实体
        combinationBaseInfoEntity = combinationBaseInfoService.getById(comBaseId);

        if (combinationBaseInfoEntity == null) {
            return CommonResult.error(201, "该组合不存在");
        } else {
            //返回组合的上架与下架状态
            combinationOnShelfFlag = combinationBaseInfoEntity.getIsUsed();
            if (combinationOnShelfFlag == 2) {
                return CommonResult.error(201, "该组合已经上架");
            } else if (combinationOnShelfFlag == 0){
                return CommonResult.error(201,"该组合已经下架,无法上架");
            } else {
                    //将组合的状态修改为上架
                    modifyCombationState = combinationBaseInfoService.modifyCombationState(comBaseId, 2);
                    if (modifyCombationState == 1) {
                        return CommonResult.success();
                    } else {
                        return CommonResult.error(201, "修改时发生异常");
                    }
                }
            }

    }

    /**
     * 组合下架
     * @param comBaseId 组合基本信息id
     * @return
     */
    @RequestMapping("/combinationOutOffShelf")
    public CommonResult combinationOutOffShelf(Long comBaseId) {
        //组合是否存在
        CombinationBaseInfoEntity combinationBaseInfoEntity;
        //组合是否下架
        Integer combinationOutOffShelfFlag;
        //是否修改组合
        Integer modifyCombationState;

        //获取组合实体
        combinationBaseInfoEntity = combinationBaseInfoService.getById(comBaseId);
        if (combinationBaseInfoEntity == null) {
            return CommonResult.error(201, "该组合不存在");
        } else {
            //返回组合的上架与下架状态
            combinationOutOffShelfFlag = combinationBaseInfoEntity.getIsUsed();
            if (combinationOutOffShelfFlag == 0) {
                return CommonResult.error(201, "该组合已经下架");
            } else if (combinationOutOffShelfFlag == 1){
                return CommonResult.error(201,"该组合还未上架");
            } else {
                //将组合的状态修改为下架
                modifyCombationState = combinationBaseInfoService.modifyCombationState(comBaseId, 0);
                if (modifyCombationState == 1){
                    return CommonResult.success();
                }else{
                    return CommonResult.error(201,"修改时发生异常");
                }
            }
        }
    }

    /**
     * 返回组合的信息
     * @param comBaseId 组合基本信息id
     * @return
     */
    @RequestMapping("/getCombinationBaseInfoEntity")
    public CommonResult getComBaseInfo(Long comBaseId){
        //组合基本信息
        CombinationBaseInfoEntity combinationBaseInfoEntity;
        //根据组合id获取组合信息
        combinationBaseInfoEntity=combinationBaseInfoService.getById(comBaseId);
        return CommonResult.success().put("combination",combinationBaseInfoEntity);
    }

    /**
     * 验证创建组合是否有重名
     * @param comName 组合的名称
     * @return
     */
    @RequestMapping("/VerifyrepeatedComName")
    public CommonResult VerifyrepeatedComName(String comName){
        //组合重名标识
        boolean  verifyFlag=false;

        verifyFlag=  combinationBaseInfoService.VerifyrepeatedComName(comName);

        if(verifyFlag){
            return CommonResult.success("没有重名");
        }else{
            return CommonResult.error(201,"有重名");
        }
    }

    /**
     * 获取组合内容详情
     * @param comBaseId
     * @param riskLevel
     * @return
     */
    @RequestMapping("/getCombinationDetailInfo")
    public  CommonResult getCombinationList(Long comBaseId,Integer riskLevel){
        ArrayList<HashMap> combinationList=new ArrayList<>();
        //获取组合列表
        combinationList=combinationBaseInfoService.getCombinationList(comBaseId,riskLevel);
        System.out.println("数据获取成功"+combinationList);
        System.out.println("风险等级为"+riskLevel);
        System.out.println("内容为"+combinationList);
        System.out.println("已经执行");
        return CommonResult.success().put("combinationList",combinationList);
    }

    /**
     * 根据收益率排行获取组合信息
     * @return
     */
    @RequestMapping("/getCombinationListsByIncome")
    public CommonResult getCombinationListByIncome(){
        ArrayList<HashMap> combinationList=new ArrayList<>();

        //获取组合列表
        combinationList=combinationBaseInfoService.getCombinationListByIncome();
        System.out.println("内容为"+combinationList);
        return CommonResult.success().put("combinationList",combinationList);
    }

    @RequestMapping("/getComBaseIdByName")
    public CommonResult getComBaseIdByName(String comBaseName){
        Long id = Long.valueOf(0);

        id = combinationBaseInfoService.getComBaseIdByName(comBaseName);

        if(id != 0){
            return CommonResult.success().put("comBaseId",id);
        }else {
            return CommonResult.error(201,"无此组合");
        }
    }
}
